/* Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com */

/*
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "scrollpanel.h"



namespace tri {
namespace ui {



extScrollPanel::extScrollPanel(): tri::ui::extPanel(){
	addComponent(&mHorizontalSlider);
	addComponent(&mVerticalSlider);
	addComponent(&mContentPanel);
	
	setStyle("DefaultScrollPanelStyle");

	mVerticalSlider.connect<extScrollPanel>(tri::VALUE_CHANGED, this,&extScrollPanel::updateVerticalPosition);
	mHorizontalSlider.connect<extScrollPanel>(tri::VALUE_CHANGED, this,&extScrollPanel::updateHorizontalPosition);
}

extScrollPanel::~extScrollPanel(){
	
}

void extScrollPanel::setContent(tri::ui::extComponent* comp){
	if(hasContent()){
		mContentPanel.removeAll();
	}
	
	mContentPanel.addComponent(comp);
	
	mVerticalSlider.setValue(0.0);
	mHorizontalSlider.setValue(0.0);
}

void extScrollPanel::update(){
	tri::ui::extPanel::update();
	
	std::vector<tri::ui::extComponent*> list;
	mContentPanel.getComponents(list);
	
	mHorizontalSlider.setVisible(false);
	mVerticalSlider.setVisible(false);
	
	float sx = mVerticalSlider.isVisible() ? getSize().x - 16 : getSize().x;
	float sy = mHorizontalSlider.isVisible() ? getSize().y - 16 : getSize().y;
	
	mContentPanel.setSize(sx, sy, 0);
	
	if (list.size() > 0){
		tri::ui::extComponent* comp = list[0];
		tri::TVector ps = getSize();
		tri::TVector cs = comp->getSize();
		
		mVerticalSlider.setVisible(cs.y > ps.y || ( ( cs.x > ps.x ) && (cs.y > ps.y-16) ) );
		mHorizontalSlider.setVisible(cs.x > ps.x || ( ( cs.y > ps.y ) && (cs.x > ps.x-16) ));
		
		float sx = mVerticalSlider.isVisible() ? getSize().x - 16 : getSize().x;
		float sy = mHorizontalSlider.isVisible() ? getSize().y - 16 : getSize().y;
		
		mContentPanel.setSize(sx, sy, 0);
		
		if (mVerticalSlider.isVisible()){
			float mv = cs.y - sy;
			float max = mVerticalSlider.getMaximumValue();
			
			if (mv != max){
				mVerticalSlider.setMaximumValue(mv);
			}
			if (mVerticalSlider.getValue() > mv){
				mVerticalSlider.setValue(mv);
			}
		}
		
		if (mHorizontalSlider.isVisible()){
			float mv = cs.x - sx;
			float max = mHorizontalSlider.getMaximumValue();
			
			if (mv != max){
				mHorizontalSlider.setMaximumValue(mv);
			}
			if (mHorizontalSlider.getValue() > mv){
				mHorizontalSlider.setValue(mv);
			}
		}
	}
	
	float posY = mHorizontalSlider.isVisible() ? 16 : 0;
	mContentPanel.setPosition(0, posY, 0);
	
	mHorizontalSlider.setSize(getSize().x-16, 16, 0);
	
	mVerticalSlider.setSize(16, getSize().y-16, 0);
	mVerticalSlider.setPosition(getSize().x-16, 16, 0);
	
	
}

void extScrollPanel::setStyle(std::string style){
	extPanel::setStyle(style);
	mContentPanel.setStyle("");
	mContentPanel.setColor(0,0,0,0);
	mVerticalSlider.setStyle(style+std::string(".VerticalSlider"));
	mHorizontalSlider.setStyle(style+std::string(".HorizontalSlider"));
}
	
bool extScrollPanel::hasContent(){
	std::vector<tri::ui::extComponent*> list;
	mContentPanel.getComponents(list);
	return list.size() > 0;
}

void extScrollPanel::updateVerticalPosition(){
	std::vector<tri::ui::extComponent*> list;
	mContentPanel.getComponents(list);
	
	if (list.size() > 0){
		tri::ui::extComponent* comp = list[0];
		tri::TVector cs = comp->getPosition();
		cs.y = -mVerticalSlider.getValue();
		comp->setPosition(cs);
	}
}

void extScrollPanel::updateHorizontalPosition(){
	std::vector<tri::ui::extComponent*> list;
	mContentPanel.getComponents(list);
	
	if (list.size() > 0){
		tri::ui::extComponent* comp = list[0];
		tri::TVector cs = comp->getPosition();
		cs.x = -mHorizontalSlider.getValue();
		comp->setPosition(cs);
	}
}


}
}
